package com.xci.core.configuration;

import com.xci.core.converter.DateConverter;
import com.xci.core.helper.FileHelper;
import com.xci.core.internal.JsonListData;
import com.xci.core.setting.UploadSetting;
import com.xci.sys.entity.SysApp;
import com.xci.sys.entity.SysChart;
import com.xci.sys.entity.SysReport;
import com.xci.sys.entity.SysWidget;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import java.util.Properties;

/**
 * 系统Bean配置
 */
@Configuration
public class BaseBeanConfig {
    @Resource
    private UploadSetting uploadSetting;

    @Bean
    public ExecutableValidator methodValidator() {
        return beanValidator().forExecutables();
    }

    @Bean
    public Validator beanValidator() {
        return Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
    }

    /**
     * 自动识别使用的数据库类型
     * 在mapper.xml中databaseId的值就是跟这里对应，
     * 如果没有databaseId选择则说明该sql适用所有数据库
     */
    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() {
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle", "oracle");
        properties.setProperty("MySQL", "mysql");
        properties.setProperty("SQL Server", "sqlserver");
        properties.setProperty("SQLite", "sqlite");
        properties.setProperty("PostgreSQL", "postgresql");
        properties.setProperty("Derby", "derby");
        properties.setProperty("H2", "h2");
        properties.setProperty("HSQL", "hsql");
        // properties.setProperty("DB2","db2");
        // properties.setProperty("Informix","informix");
        // properties.setProperty("Sybase","sybase");
        // properties.setProperty("Hana","hana");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

    @Bean
    public DateConverter dateConverter() {
        return new DateConverter();
    }

    //regeion JsonListData

    /**
     * 报表数据文件
     */
    @Bean("reportJsonList")
    public JsonListData<SysReport> createReportJsonListData(){
        var filePath = uploadSetting.buildPath("/report/reports.json");
        return new JsonListData<>(FileHelper.buildRootFile(filePath),SysReport.class);
    }

    /**
     * 图表数据文件
     */
    @Bean("chartJsonList")
    public JsonListData<SysChart> createChartJsonListData(){
        var filePath = uploadSetting.buildPath("/chart/charts.json");
        return new JsonListData<>(FileHelper.buildRootFile(filePath),SysChart.class);
    }

    /**
     * 应用数据文件
     */
    @Bean("appJsonList")
    public JsonListData<SysApp> createAppJsonListData(){
        var filePath = uploadSetting.buildPath("/app/apps.json");
        return new JsonListData<>(FileHelper.buildRootFile(filePath),SysApp.class);
    }

    /**
     * 部件数据文件
     */
    @Bean("widgetJsonList")
    public JsonListData<SysWidget> createWidgetJsonListData(){
        var filePath = uploadSetting.buildPath("/widget/widgets.json");
        return new JsonListData<>(FileHelper.buildRootFile(filePath),SysWidget.class);
    }

    //endregion
}